﻿@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>角色管理</title>
    <link href="~/Scripts/jquery-easyui-1.4.4/themes/bootstrap/easyui.css" rel="stylesheet" />
    <script src="~/Scripts/jquery-easyui-1.4.4/jquery.min.js"></script>
    <script src="~/Scripts/jquery-easyui-1.4.4/jquery.easyui.min.js"></script>
    <script src="~/Scripts/jquery-easyui-1.4.4/locale/easyui-lang-zh_CN.js"></script>
    <link href="~/Content/themes/icon.css" rel="stylesheet" />
    <script src="~/Content/js/common.js"></script>
    <link href="~/Content/themes/table.css" rel="stylesheet" />
    <script type="text/javascript">
        $(function () {
            $.ajax({     //请求当前用户可以操作的按钮
                url: "/Button/GetUserAuthorizeButton?r=" + Math.random(),
                type: "post",
                data: { "KeyCode": "role", "KeyName": "Role" },
                dataType: "json",
                timeout: 5000,
                success: function (data) {
                    if (data.success) {
                        var toolbar = getToolBar(data);      //common.js
                        if (data.search) {     //判断是否有浏览权限
                            var oldSelectRoleId;   //上次点选的RoleId
                            $("#ui_role_dg").datagrid({       //初始化datagrid
                                url: "/Role/GetAllRoleInfo",
                                striped: true, rownumbers: true, pagination: true, pageSize: 20, singleSelect: true,
                                idField: 'Id',
                                sortName: 'UpdateTime',
                                sortOrder: 'asc',
                                pageList: [20, 40, 60, 80, 100],
                                frozenColumns: [[
                                                //{ field: 'ck', checkbox: true },
                                                 {
                                                     width: '100',
                                                     title: '角色名称',
                                                     field: 'RoleName',
                                                     formatter: function (value, row, index) {
                                                         return value.length > 8 ? '<span title="' + value + '">' + value + '</span>' : value;
                                                     }
                                                 }]],
                                columns: [[
                                           { field: 'UpdateTime', title: '最后更新时间', sortable: true, width: 130 },
                                           { field: 'UpdateBy', title: '最后更新人', width: 130 },
                                           {
                                               field: 'Description', title: '角色描述', width: 300,
                                               formatter: function (value, row, index) {
                                                   return value.length > 20 ? '<span title="' + value + '">' + value + '</span>' : value;
                                               }
                                           }]],
                                toolbar: toolbar.length == 0 ? null : toolbar,
                                onSelect: function (rowIndex, rowData) {
                                    if (oldSelectRoleId == rowData.Id) {
                                        return;
                                    }
                                    oldSelectRoleId = rowData.Id;
                                    var $ui_role_layout = $("#ui_role_layout");
                                    var eastRoleUser = $ui_role_layout.layout("panel", "east");
                                    if (eastRoleUser.panel("options").collapsed) {   //判断是否展开
                                        $ui_role_layout.layout("expand", "east");
                                    }
                                    eastRoleUser.panel("setTitle", rowData.RoleName + "成员");
                                    if ($("#ui_role_user_dg").data("datagrid")) {
                                        $("#ui_role_user_dg").datagrid({       //请求数据
                                            url: "/Role/GetRoleUserByRoleId?roleId=" + rowData.Id
                                        });
                                    }
                                    else {
                                        $("#ui_role_user_dg").datagrid({       //初始化datagrid
                                            url: "/Role/GetRoleUserByRoleId?roleId=" + rowData.Id,
                                            striped: true, rownumbers: true, pagination: true, pageSize: 20, singleSelect: true,
                                            idField: 'ID',
                                            sortName: 'CreateTime',
                                            sortOrder: 'asc',
                                            pageList: [20, 40, 60, 80, 100],
                                            columns: [[
                                                  { field: 'AccountName', title: '登录名', sortable: true, width: 100 },
                                                  { field: 'RealName', title: '姓名', sortable: true, width: 80 },
                                                  {
                                                      field: 'IsAble', title: '启用', sortable: true, width: 60, align: 'center',
                                                      formatter: function (value, row, index) {
                                                          if (value != null) {
                                                              return value ? '<img src="../Content/themes/icon/chk_checked.gif" alt="已启用" title="用户已启用" />' : '<img src="../Content/themes/icon/chk_unchecked.gif" alt="未启用" title="用户未启用" />';
                                                          }
                                                      }
                                                  },
                                                  {
                                                      field: 'IfChangePwd', title: '改密', sortable: true, width: 60, align: 'center',
                                                      formatter: function (value, row, index) {
                                                          if (value != null) {
                                                              return value ? '<img src="../Content/themes/icon/chk_checked.gif" alt="已改密" title="用户已改密" />' : '<img src="../Content/themes/icon/chk_unchecked.gif" alt="未改密" title="用户未改密" />';
                                                          }
                                                      }
                                                  },
                                                  { field: 'CreateTime', title: '添加时间', sortable: true, width: 130 }]]
                                        });
                                    }
                                }
                            });
                        }
                        else {
                            $.show_alert("提示", "无权限，请联系管理员！");
                        }
                    } else {
                        $.show_alert("错误", data.result);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    if (textStatus == "timeout") {
                        $.show_alert("提示", "请求超时，请刷新当前页重试！");
                    }
                    else {
                        $.show_alert("错误", textStatus + "：" + errorThrown);
                    }
                }
            })
        });

        //添加角色
        function AddRole() {
            $("<div/>").dialog({
                id: "ui_role_add_dialog",
                href: "/Role/RoleAdd",
                title: "添加角色",
                height: 250,
                width: 400,
                modal: true,
                buttons: [{
                    id: "ui_role_add_btn",
                    text: '添 加',
                    handler: function () {
                        $("#roleaddform").form("submit", {
                            url: "/Role/AddRole",
                            onSubmit: function (param) {
                                $('#ui_role_add_btn').linkbutton('disable');
                                param.RoleName = $("#txtRoleNameAdd").val();
                                param.Description = $("#txtRoleDescriptionAdd").val();
                                if ($(this).form('validate'))
                                    return true;
                                else {
                                    $('#ui_role_add_btn').linkbutton('enable');
                                    return false;
                                }
                            },
                            success: function (data) {
                                var dataJson = eval('(' + data + ')');
                                if (dataJson.success) {
                                    $("#ui_role_add_dialog").dialog('destroy');
                                    $.show_alert("提示", dataJson.msg);
                                    $("#ui_role_dg").datagrid("reload");
                                } else {
                                    $('#ui_role_add_btn').linkbutton('enable');
                                    $.show_alert("提示", dataJson.msg);
                                }
                            }
                        });
                    }
                }],
                onLoad: function () {
                    $("#txtRoleNameAdd").focus();
                },
                onClose: function () {
                    $("#ui_role_add_dialog").dialog('destroy');  //销毁dialog对象
                }
            });
        }

        //修改角色
        function EditRole() {
            var row = $("#ui_role_dg").datagrid("getChecked");
            if (row.length < 1) {
                $.show_alert("提示", "请先选择要修改的角色");
                return;
            }
            if (row.length > 1) {
                $.show_alert("提示", "不支持批量修改角色");
                $("#ui_role_dg").datagrid('clearSelections').datagrid('clearChecked');
                return;
            }
            $("<div/>").dialog({
                id: "ui_role_edit_dialog",
                href: "/Role/RoleEdit",
                title: "修改角色",
                height: 250,
                width: 400,
                modal: true,
                buttons: [{
                    id: "ui_role_edit_btn",
                    text: '修 改',
                    handler: function () {
                        $("#roleeditform").form("submit", {
                            url: "/Role/EditRole",
                            onSubmit: function (param) {
                                $('#ui_role_edit_btn').linkbutton('disable');
                                param.id = $("#hidid").val();
                                param.originalName = $("#hidoriginalName").val();
                                param.RoleName = $("#txtRoleNameEdit").val();
                                param.Description = $("#txtRoleDescriptionEdit").val();
                                if ($(this).form('validate'))
                                    return true;
                                else {
                                    $('#ui_role_edit_btn').linkbutton('enable');
                                    return false;
                                }
                            },
                            success: function (data) {
                                var dataJson = eval('(' + data + ')');
                                if (dataJson.success) {
                                    $("#ui_role_edit_dialog").dialog('destroy');
                                    $.show_alert("提示", dataJson.msg);
                                    $("#ui_role_dg").datagrid("reload");
                                } else {
                                    $('#ui_role_edit_btn').linkbutton('enable');
                                    $.show_alert("提示", dataJson.msg);
                                }
                            }
                        });
                    }
                }],
                onLoad: function () {
                    $("#hidid").val(row[0].Id);
                    $("#hidoriginalName").val(row[0].RoleName);
                    $("#txtRoleNameEdit").val(row[0].RoleName);
                    $("#txtRoleDescriptionEdit").val(row[0].Description);
                },
                onClose: function () {
                    $("#ui_role_edit_dialog").dialog('destroy');  //销毁dialog对象
                }
            });
        }

        //删除角色
        function DelRole() {
            var rows = $("#ui_role_dg").datagrid("getChecked");
            if (rows.length < 1) {
                $.show_alert("提示", "请先勾选要删除的角色");
                return;
            }
            if (rows.length > 1) {
                $.show_alert("提示", "不支持批量删除");
                $("#ui_role_dg").datagrid('clearSelections').datagrid('clearChecked');
                return;
            }
            $.messager.confirm('提示', '确定删除：' + rows[0].RoleName + '？', function (r) {
                if (r) {
                    var RoleIds = "";
                    $.each(rows, function (i, row) {
                        RoleIds += row.Id + ",";
                    });
                    RoleIds = RoleIds.substring(0, RoleIds.length - 1);
                    $.ajax({
                        url: "/Role/DelRoleByIDs",
                        data: {
                            IDs: RoleIds
                        },
                        type: "POST",
                        dataType: "json",
                        success: function (data) {
                            if (data.success) {
                                $.show_alert("提示", data.msg);
                                $("#ui_role_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');;
                                //如果“用户管理”标签页处于打开状态需要刷新，否则被删除的角色还显示，再点编辑用户就出错
                                if ($('#tabs').tabs('exists', '用户管理')) {
                                    $('#tabs').tabs('getTab', '用户管理').panel('refresh');
                                }
                                $("#ui_role_layout").layout("collapse", "east");
                                $("#ui_role_user_dg").datagrid('loadData', { total: 0, rows: [] });   //清空
                            } else {
                                $.show_alert("提示", data.msg);
                            }
                        }
                    });
                }
            });
        }

        //角色授权
        function RoleAuthorize() {
            var row = $("#ui_role_dg").datagrid("getChecked");
            if (row.length < 1) {
                $.show_alert("提示", "请先勾选要授权的角色");
                return;
            }
            if (row.length > 1) {
                $.show_alert("提示", "不支持批量角色授权");
                $("#ui_role_dg").datagrid('clearSelections').datagrid('clearChecked');
                return;
            }

            //初始化dialog
            $("<div/>").dialog({
                id: "ui_role_authorize_dialog",
                href: "/Role/RoleMenu",
                title: "角色授权：" + row[0].RoleName,
                height: 500,
                width: 300,
                modal: true,
                buttons: [{
                    id: "ui_role_authorize_btn",
                    text: '授 权',
                    handler: function () {
                        $('#ui_role_authorize_btn').linkbutton('disable');
                        doAuthorizeNew(row[0].Id);
                    }
                }],
                onLoad: function () {
                    $('#treerolemenu').tree({
                        url: "/Menu/GetAllRoleMenuButtonTree?roleid=" + row[0].Id + "&r=" + Math.random(),
                        checkbox: true,
                        loadFilter: function (data) {
                            return data;
                        }
                    });
                },
                onClose: function () {
                    $("#ui_role_authorize_dialog").dialog('destroy');  //销毁dialog对象
                }
            });
        }
        //保存按钮授权
        function doAuthorizeNew(roleId) {   //保存授权信息
            var menuButtonId = "";
            var nodes = $('#treerolemenu').tree('getChecked');
            for (var i = 0; i < nodes.length; i++) {
                menuButtonId += nodes[i].attributes.menuid + " " + nodes[i].attributes.buttonid + ",";
            }
            //取出来子级选中 但父级未选中的ID集合
            var nodes2 = $('#treerolemenu').tree('getChecked', 'indeterminate');
            for (var i = 0; i < nodes2.length; i++) {
                menuButtonId += nodes2[i].attributes.menuid + " " + nodes2[i].attributes.buttonid + ",";
            }

            var para = {};
            para.timespan = new Date().getTime();
            para.menuButtonId = menuButtonId;
            para.roleId = roleId;

            $.ajax({
                url: "/Role/SetRoleMenuButton",
                data: para,
                type: "POST",
                dataType: "json",
                success: function (data) {
                    if (data.success) {
                        $("#ui_role_authorize_dialog").dialog('destroy');  //销毁dialog对象
                        $.show_alert("提示", data.msg);
                    } else {
                        $('#ui_role_authorize_btn').linkbutton('enable');
                        $.show_alert("提示", data.msg);
                    }
                }
            });
        }

        //搜索
        function searchdata() {
            $("#ui_role_dg").datagrid('load', {
                RoleName: $("#txtSearchRoleName").val()
            });
            $("#ui_role_dg").datagrid('clearSelections').datagrid('clearChecked');
        }
    </script>
</head>
<body>
    <div id="ui_role_layout" class="easyui-layout" data-options="fit:true,border:false">
        <div data-options="region:'north',split:true,border:true,collapsed:false" title="搜索条件" style="height:72px;">
            <div id="ui_role_search">
                <table class="tableForm" style="height: 28px; background: #f5f5f5;">
                    <tr>
                        <th height="20px">
                            角色名称：
                        </th>
                        <td>
                            <input name="txtSearchRoleName" id="txtSearchRoleName" class="easyui-validatebox textbox" style="width:150px;height:22px;" />
                        </td>
                        <td>
                            <a href="javascript:void(0);" class="easyui-linkbutton" iconcls="icon-search" plain="true"
                               onclick="searchdata();">搜索</a>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
        <div data-options="region:'east',split:true,border:true,collapsed:true" title="角色成员"
             style="width: 480px;">
            <table id="ui_role_user_dg" data-options="fit:true,border:false"></table>
        </div>
        <div data-options="region:'center',border:false">
            <table id="ui_role_dg" data-options="fit:true,border:false"></table>
        </div>
    </div>

</body>
</html>
